<?
if (!defined('SMF'))
	die('Hacking attempt...');

// Display the planet transfer form
function DisplayTransferForm($playerinfo,$planetinfo)
{
	global $txt;

	$max_holds = NUM_HOLDS($playerinfo['hull']);
	$free_holds = NUM_HOLDS($playerinfo['hull']) - $playerinfo['ship_ore'] - $playerinfo['ship_organics'] - $playerinfo['ship_goods'] - $playerinfo['ship_colonists'];
	$max_power = NUM_ENERGY($playerinfo['power']);
	$free_power = NUM_ENERGY($playerinfo['power']) - $playerinfo['ship_energy'];
	$max_fighters = NUM_FIGHTERS($playerinfo['computer']);
	$free_fighters = NUM_FIGHTERS($playerinfo['computer']) - $playerinfo['ship_fighters'];
	$max_torps = NUM_TORPEDOES($playerinfo['torp_launchers']);
	$free_torps = NUM_TORPEDOES($playerinfo['torp_launchers']) - $playerinfo['torps'];

	$txt['bnt_planet_cinfo'] = str_replace("[cargo]", Number($free_holds), $txt['bnt_planet_cinfo']);
	$txt['bnt_planet_cinfo'] = str_replace("[energy]", Number($free_power), $txt['bnt_planet_cinfo']);

	echo '
			<div id="planet_transfer_header">
				<div id="cargospace_frame">
					<div class="overlay"></div>
					<div id="cargospace_content">
						<h4>', $txt['bnt_planet_cargospace'], '</h4>
						<span id="free_holds">', Number($free_holds), '</span>
					</div>
				</div>	
				<div id="powercap_frame">
					<div class="overlay"></div>
					<div id="powercap_content">
						<h4>', $txt['bnt_planet_powercap'], '</h4>
						<span id="free_power">', Number($free_power), '</span>
					</div>
				</div>
			</div>
			<div class="clearfix"></div>
			<script type="text/javascript">
				$(document).ready(function(){
					// Table sorting function
					$(".bnt_table").tablesorter({
						headers: {
							0: {sorter: false},
							1: {sorter: false},
							2: {sorter: false},
							3: {sorter: false},
							4: {sorter: false},
							5: {sorter: false},
							6: {sorter: false}
						},
						widgets: ["zebra"]
					});
					
					// Setup the commodities object so we can work with all the numbers dynamically
					$.commodities = {
						max_holds: ', $max_holds, ',
						free_holds: ', $free_holds, ',
						max_power: ', $max_power, ',
						free_power: ', $free_power, ',
						max_fighters: ', $max_fighters, ',
						free_fighters: ', $free_fighters, ',
						max_torps: ', $max_torps, ',
						free_torps: ', $free_torps, ',
						ship_ore: ', $playerinfo['ship_ore'], ',
						ship_organics: ', $playerinfo['ship_organics'], ',
						ship_goods: ', $playerinfo['ship_goods'], ',
						ship_colonists: ', $playerinfo['ship_colonists'], ',
						ship_energy: ', $playerinfo['ship_energy'], ',
						ship_fighters: ', $playerinfo['ship_fighters'], ',
						ship_torps: ', $playerinfo['torps'], ',
						ship_credits: ', $playerinfo['credits'], ',
						planet_ore: ', $planetinfo['ore'], ',
						planet_organics: ', $planetinfo['organics'], ',
						planet_goods: ', $planetinfo['goods'], ',
						planet_colonists: ', $planetinfo['colonists'], ',
						planet_energy: ', $planetinfo['energy'], ',
						planet_fighters: ', $planetinfo['fighters'], ',
						planet_torps: ', $planetinfo['torps'], ',
						planet_credits: ', $planetinfo['credits'], '
					}
					
					// Max - Min commodity buttons
					$(".max_toplanet,.max_toship").click(function() { 
						// Get the commodity type from the title inside the table
						var parent = $(this).parent();
						var commodity = parent.prevAll(".commodity_type").html().toLowerCase();
						commodity = $.trim(commodity);
						
						// Max to planet button
						if ($(this).hasClass("max_toplanet"))
						{
							var ship_col = parent.nextAll(".ship_commodity_col");
							var transfer = stripCommas(ship_col.children("span").html()) * -1;
						}
						// Max to ship button
						else
						{
							var planet_col = parent.prevAll(".planet_commodity_col");
							var transfer = stripCommas(planet_col.children("span").html()) * 1;
						}

						// Switch out to the appropriate function depending on the commodity type
						switch(commodity)
						{
							case "ore":
								UpdateOre(transfer);
								break;
							case "organics":
								UpdateOrganics(transfer);
								break;
							case "goods":
								UpdateGoods(transfer);
								break;
							case "colonists":
								UpdateColonists(transfer);
								break;
							case "energy":
								UpdateEnergy(transfer);
								break;
							case "torpedoes":
								UpdateTorps(transfer);
								break;
							case "fighters":
								UpdateFighters(transfer);
								break;
							case "credits":
								UpdateCredits(transfer);
								break;
							default:
								alert("commodity not found");
						}
					});
					
					// Get manual user input transfer request
					// Let the update resource functions handle validation
					$("input[id^=\'manual_transfer\']").change(function() { 
						// Get the commodity type from the title inside the table
						var parent = $(this).parent();
						var commodity = parent.prevAll(".commodity_type").html().toLowerCase();
						commodity = $.trim(commodity);
						
						var transfer = stripCommas($(this).val()) * 1;

						// Switch out to the appropriate function depending on the commodity type
						switch(commodity)
						{
							case "ore":
								UpdateOre(transfer);
								break;
							case "organics":
								UpdateOrganics(transfer);
								break;
							case "goods":
								UpdateGoods(transfer);
								break;
							case "colonists":
								UpdateColonists(transfer);
								break;
							case "energy":
								UpdateEnergy(transfer);
								break;
							case "torpedoes":
								UpdateTorps(transfer);
								break;
							case "fighters":
								UpdateFighters(transfer);
								break;
							case "credits":
								UpdateCredits(transfer);
								break;
						}
					});
					
					// Ore Resource Slider
					$("#slider_ore").slider({
						animate: true,
						range: "max",
						min: -', $playerinfo['ship_ore'], ',
						max: ', $planetinfo['ore'] > $free_holds ? $free_holds : $planetinfo['ore'],',
						value: 0,
						slide: function(event, ui) {
							// Update transfer input field
							$("#manual_transfer_ore").val(addCommas(ui.value));
							
							// Update commodity numbers
							$("#planet_ore").html(addCommas(', $planetinfo['ore'], ' - ui.value));
							$.commodities.planet_ore = ', $planetinfo['ore'], ' - ui.value;
							$("#ship_ore").html(addCommas(', $playerinfo['ship_ore'], ' + ui.value));
							$.commodities.ship_ore = ', $playerinfo['ship_ore'], ' + ui.value;
							
							// Recalculate ships free holds
							$.commodities.free_holds = $.commodities.max_holds - $.commodities.ship_ore - $.commodities.ship_organics - $.commodities.ship_goods - $.commodities.ship_colonists;
							
							// Update free holds display
							$("#free_holds").html(addCommas($.commodities.free_holds));
																	
							// Update other commodity sliders that use the ships holds
							UpdateCargoSliders("ore");
						}
					});
					
					// Organics Resource Slider
					$("#slider_organics").slider({
						animate: true,
						range: "max",
						min: -', $playerinfo['ship_organics'], ',
						max: ', $planetinfo['organics'] > $free_holds ? $free_holds : $planetinfo['organics'],',
						value: 0,
						slide: function(event, ui) {
							// Update transfer input field
							$("#manual_transfer_organics").val(addCommas(ui.value));
							
							// Update commodity numbers
							$("#planet_organics").html(addCommas(', $planetinfo['organics'], ' - ui.value));
							$.commodities.planet_organics = ', $planetinfo['organics'], ' - ui.value;
							$("#ship_organics").html(addCommas(', $playerinfo['ship_organics'], ' + ui.value));
							$.commodities.ship_organics = ', $playerinfo['ship_organics'], ' + ui.value;
							
							// Recalculate ships free holds
							$.commodities.free_holds = $.commodities.max_holds - $.commodities.ship_ore - $.commodities.ship_organics - $.commodities.ship_goods - $.commodities.ship_colonists;
							
							// Update free holds display
							$("#free_holds").html(addCommas($.commodities.free_holds));
							
							// Update other commodity sliders that use the ships holds
							UpdateCargoSliders("organics");
						}
					});
					
					// Goods Resource Slider
					$("#slider_goods").slider({
						animate: true,
						range: "max",
						min: -', $playerinfo['ship_goods'], ',
						max: ', $planetinfo['goods'] > $free_holds ? $free_holds : $planetinfo['goods'],',
						value: 0,
						slide: function(event, ui) {
							// Update transfer input field
							$("#manual_transfer_goods").val(addCommas(ui.value));
							
							// Update commodity numbers
							$("#planet_goods").html(addCommas(', $planetinfo['goods'], ' - ui.value));
							$.commodities.planet_goods = ', $planetinfo['goods'], ' - ui.value;
							$("#ship_goods").html(addCommas(', $playerinfo['ship_goods'], ' + ui.value));
							$.commodities.ship_goods = ', $playerinfo['ship_goods'], ' + ui.value;
							
							// Recalculate ships free holds
							$.commodities.free_holds = $.commodities.max_holds - $.commodities.ship_ore - $.commodities.ship_organics - $.commodities.ship_goods - $.commodities.ship_colonists;
							
							// Update free holds display
							$("#free_holds").html(addCommas($.commodities.free_holds));
							
							// Update other commodity sliders that use the ships holds
							UpdateCargoSliders("goods");
						}
					});
					
					// Colonists Resource Slider
					$("#slider_colonists").slider({
						animate: true,
						range: "max",
						min: -', $playerinfo['ship_colonists'], ',
						max: ', $planetinfo['colonists'] > $free_holds ? $free_holds : $planetinfo['colonists'],',
						value: 0,
						slide: function(event, ui) {
							// Update transfer input field
							$("#manual_transfer_colonists").val(addCommas(ui.value));
							
							// Update commodity numbers
							$("#planet_colonists").html(addCommas(', $planetinfo['colonists'], ' - ui.value));
							$.commodities.planet_colonists = ', $planetinfo['colonists'], ' - ui.value;
							$("#ship_colonists").html(addCommas(', $playerinfo['ship_colonists'], ' + ui.value));
							$.commodities.ship_colonists = ', $playerinfo['ship_colonists'], ' + ui.value;
							
							// Recalculate ships free holds
							$.commodities.free_holds = $.commodities.max_holds - $.commodities.ship_ore - $.commodities.ship_organics - $.commodities.ship_goods - $.commodities.ship_colonists;
							
							// Update free holds display
							$("#free_holds").html(addCommas($.commodities.free_holds));
							
							// Update other commodity sliders that use the ships holds
							UpdateCargoSliders("colonists");
						}
					});
					
					// Transfer energy requires a check on the maximum ship values
					$("#slider_energy").slider({
						animate: true,
						range: "max",
						min: -', $playerinfo['ship_energy'], ',
						max: ', $planetinfo['energy'] > $free_power ? $free_power : $planetinfo['energy'], ',
						value: 0,
						slide: function(event, ui) {
							// Update transfer input field
							$("#manual_transfer_energy").val(addCommas(ui.value));
							
							// Update commodity numbers
							$("#planet_energy").html(addCommas(', $planetinfo['energy'], ' - ui.value));
							$.commodities.planet_energy = ', $planetinfo['energy'], ' - ui.value;
							$("#ship_energy").html(addCommas(', $playerinfo['ship_energy'], ' + ui.value));
							$.commodities.ship_energy = ', $playerinfo['ship_energy'], ' + ui.value;
							
							// Update free power display
							$.commodities.free_power = $.commodities.max_power - $.commodities.ship_energy;
							$("#free_power").html(addCommas($.commodities.free_power));
						}
					});
					
					// Transfer fighters requires a check on the maximum ship values
					$("#slider_fighters").slider({
						animate: true,
						range: "max",
						min: -', $playerinfo['ship_fighters'], ',
						max: ', $planetinfo['fighters'] > $max_fighters ? $max_fighters - $playerinfo['ship_fighters'] : $planetinfo['fighters,'], ',
						value: 0,
						slide: function(event, ui) {
							// Update transfer input field
							$("#manual_transfer_fighters").val(addCommas(ui.value));
							
							// Update commodity numbers
							$("#planet_fighters").html(addCommas(', $planetinfo['fighters'], ' - ui.value));
							$.commodities.planet_fighters = ', $planetinfo['fighters'], ' - ui.value;
							$("#ship_fighters").html(addCommas(', $playerinfo['ship_fighters'], ' + ui.value));
							$.commodities.ship_fighters = ', $playerinfo['ship_fighters'], ' + ui.value;
						}
					});
					
					// Transfer torps requires a check on the maximum ship values
					$("#slider_torps").slider({
						animate: true,
						range: "max",
						min: -', $playerinfo['torps'], ',
						max: ', $planetinfo['torps'] > $max_torps ? $max_torps - $playerinfo['torps'] : $planetinfo['torps,'], ',
						value: 0,
						slide: function(event, ui) {
							// Update transfer input field
							$("#manual_transfer_torps").val(addCommas(ui.value));
							
							// Update commodity numbers
							$("#planet_torps").html(addCommas(', $planetinfo['torps'], ' - ui.value));
							$.commodities.planet_torps = ', $planetinfo['torps'], ' - ui.value;
							$("#ship_torps").html(addCommas(', $playerinfo['torps'], ' + ui.value));
							$.commodities.ship_torps = ', $playerinfo['torps'], ' + ui.value;
						}
					});
					
					// Transfer credits slider is simple, no planet or ship maximums to worry about
					$("#slider_credits").slider({
						animate: true,
						range: "max",
						min: -', $playerinfo['credits'], ',
						max: ', $planetinfo['credits'], ',
						value: 0,
						slide: function(event, ui) {
							// Update transfer input field
							$("#manual_transfer_credits").val(addCommas(ui.value));
							
							// Update commodity numbers
							$("#planet_credits").html(addCommas(', $planetinfo['credits'], ' - ui.value));
							$.commodities.planet_credits = ', $planetinfo['credits'], ' - ui.value;
							$("#ship_credits").html(addCommas(', $playerinfo['credits'], ' + ui.value));
							$.commodities.ship_credits = ', $playerinfo['credits'], ' + ui.value;
						}
					});
					
					// Manual Ore update
					function UpdateOre(transfer_ore)
					{	
						// Check if the player tries to transfer more of the commodity to his ship than is on the planet
						if (transfer_ore > $.commodities.planet_ore)
						{
							// Check if the player is trying to transfer more of the commoditiy to his ship than he has hold space for
							if (transfer_ore > $.commodities.free_holds)
							{
								// Update the transfer amount to whatever the player can hold
								transfer_ore =  $.commodities.free_holds;
								
								// Update the planet value
								$.commodities.planet_ore = $.commodities.planet_ore * 1 - transfer_ore * 1;
								
								// Updaet ship value
								$.commodities.ship_ore = $.commodities.ship_ore * 1 + transfer_ore * 1;
							}
							// Otherwise, update ship commodity
							else
							{
								// Set the ship to the current commodity load plus everything on the planet
								$.commodities.ship_ore = $.commodities.planet_ore * 1 + $.commodities.ship_ore * 1;
								
								// Set the planet commodity to 0
								$.commodities.planet_ore = 0;
							}
						}
						// Check if the player tries to transfer more of the commodity to the planet than is on his ship
						else if (transfer_ore < -(', $playerinfo['ship_ore'], ' * 1))
						{
							transfer_ore = $.commodities.ship_ore * -1;
							
							// Update planet commodity
							$.commodities.planet_ore = $.commodities.planet_ore * 1 + $.commodities.ship_ore * 1;
							
							// Update ship commodity
							$.commodities.ship_ore = 0;
						}
						// Check if the player tries to transfer more of the commodity to his ship than he has holds for
						else if (transfer_ore > $.commodities.free_holds)
						{
							transfer_ore = $.commodities.free_holds;
							
							// Update planet commodity
							$.commodities.planet_ore = $.commodities.planet_ore - transfer_ore * 1;
							
							// Update ship commodity
							$.commodities.ship_ore = $.commodities.ship_ore + transfer_ore * 1;
						}
						// Else the player is transferring a legal value
						else
						{
							// Update planet commodity
							$.commodities.planet_ore = ', $planetinfo['ore'], ' - transfer_ore * 1;
							
							// Update ship commodity
							$.commodities.ship_ore = ', $playerinfo['ship_ore'], ' + transfer_ore * 1;
						}
						
						// Update planet commodity display
						$("#planet_ore").html(addCommas($.commodities.planet_ore));
						
						// Update ship commodity display
						$("#ship_ore").html(addCommas($.commodities.ship_ore));
						
						// Update this slider with the adjusted transfer amount, relative to the ship load
						$("#slider_ore").slider("option", "value", $.commodities.ship_ore * 1 - ', $playerinfo['ship_ore'], ' * 1);
						
						// Update the transfer input field with the adjusted transfer amount, relative the ship load
						$("#manual_transfer_ore").val(addCommas($.commodities.ship_ore * 1 - ', $playerinfo['ship_ore'], ' * 1));
						
						// Update free holds display
						$.commodities.free_holds = $.commodities.max_holds - $.commodities.ship_ore - $.commodities.ship_organics - $.commodities.ship_goods - $.commodities.ship_colonists;
						$("#free_holds").html(addCommas($.commodities.free_holds));
						
						// Update other commodity sliders that use the ships holds
						UpdateCargoSliders("ore");
					}
					
					// Manual Organics update
					function UpdateOrganics(transfer_organics)
					{	
						// Check if the player tries to transfer more of the commodity to his ship than is on the planet
						if (transfer_organics > $.commodities.planet_organics)
						{
							// Check if the player is trying to transfer more of the commoditiy to his ship than he has hold space for
							if (transfer_organics > $.commodities.free_holds)
							{
								// Update the transfer amount to whatever the player can hold
								transfer_organics =  $.commodities.free_holds;
								
								// Update the planet value
								$.commodities.planet_organics = $.commodities.planet_organics * 1 - transfer_organics * 1;
								
								// Updaet ship value
								$.commodities.ship_organics = $.commodities.ship_organics * 1 + transfer_organics * 1;
							}
							// Otherwise, update ship commodity
							else
							{
								// Set the ship to the current commodity load plus everything on the planet
								$.commodities.ship_organics = $.commodities.planet_organics * 1 + $.commodities.ship_organics * 1;
								
								// Set the planet commodity to 0
								$.commodities.planet_organics = 0;
							}
						}
						// Check if the player tries to transfer more of the commodity to the planet than is on his ship
						else if (transfer_organics < -(', $playerinfo['ship_organics'], ' * 1))
						{
							transfer_organics = $.commodities.ship_organics * -1;
							
							// Update planet commodity
							$.commodities.planet_organics = $.commodities.planet_organics * 1 + $.commodities.ship_organics * 1;
							
							// Update ship commodity
							$.commodities.ship_organics = 0;
						}
						// Check if the player tries to transfer more of the commodity to his ship than he has holds for
						else if (transfer_organics > $.commodities.free_holds)
						{
							transfer_organics = $.commodities.free_holds;
							
							// Update planet commodity
							$.commodities.planet_organics = $.commodities.planet_organics - transfer_organics * 1;
							
							// Update ship commodity
							$.commodities.ship_organics = $.commodities.ship_organics + transfer_organics * 1;
						}
						// Else the player is transferring a legal value
						else
						{
							// Update planet commodity
							$.commodities.planet_organics = ', $planetinfo['organics'], ' - transfer_organics * 1;
							
							// Update ship commodity
							$.commodities.ship_organics = ', $playerinfo['ship_organics'], ' + transfer_organics * 1;
						}
						
						// Update free holds display
						$.commodities.free_holds = $.commodities.max_holds - $.commodities.ship_ore - $.commodities.ship_organics - $.commodities.ship_goods - $.commodities.ship_colonists;
						$("#free_holds").html(addCommas($.commodities.free_holds));
						
						// Update planet commodity display
						$("#planet_organics").html(addCommas($.commodities.planet_organics));
						
						// Update ship commodity display
						$("#ship_organics").html(addCommas($.commodities.ship_organics));
						
						// Update this slider with the adjusted transfer amount, relative to the ship load
						$("#slider_organics").slider("option", "value", $.commodities.ship_organics * 1 - ', $playerinfo['ship_organics'], ' * 1);
						
						// Update the transfer input field with the adjusted transfer amount, relative the ship load
						$("#manual_transfer_organics").val(addCommas($.commodities.ship_organics * 1 - ', $playerinfo['ship_organics'], ' * 1));
						
						// Update other commodity sliders that use the ships holds
						UpdateCargoSliders("organics");
					}
					
					// Manual Goods update
					function UpdateGoods(transfer_goods)
					{	
						// Check if the player tries to transfer more of the commodity to his ship than is on the planet
						if (transfer_goods > $.commodities.planet_goods)
						{
							// Check if the player is trying to transfer more of the commoditiy to his ship than he has hold space for
							if (transfer_goods > $.commodities.free_holds)
							{
								// Update the transfer amount to whatever the player can hold
								transfer_goods =  $.commodities.free_holds;
								
								// Update the planet value
								$.commodities.planet_goods = $.commodities.planet_goods * 1 - transfer_goods * 1;
								
								// Updaet ship value
								$.commodities.ship_goods = $.commodities.ship_goods * 1 + transfer_goods * 1;
							}
							// Otherwise, update ship commodity
							else
							{
								// Set the ship to the current commodity load plus everything on the planet
								$.commodities.ship_goods = $.commodities.planet_goods * 1 + $.commodities.ship_goods * 1;
								
								// Set the planet commodity to 0
								$.commodities.planet_goods = 0;
							}
						}
						// Check if the player tries to transfer more of the commodity to the planet than is on his ship
						else if (transfer_goods < -(', $playerinfo['ship_goods'], ' * 1))
						{
							transfer_goods = $.commodities.ship_goods * -1;
							
							// Update planet commodity
							$.commodities.planet_goods = $.commodities.planet_goods * 1 + $.commodities.ship_goods * 1;
							
							// Update ship commodity
							$.commodities.ship_goods = 0;
						}
						// Check if the player tries to transfer more of the commodity to his ship than he has holds for
						else if (transfer_goods > $.commodities.free_holds)
						{
							transfer_goods = $.commodities.free_holds;
							
							// Update planet commodity
							$.commodities.planet_goods = $.commodities.planet_goods - transfer_goods * 1;
							
							// Update ship commodity
							$.commodities.ship_goods = $.commodities.ship_goods + transfer_goods * 1;
						}
						// Else the player is transferring a legal value
						else
						{
							// Update planet commodity
							$.commodities.planet_goods = ', $planetinfo['goods'], ' - transfer_goods * 1;
							
							// Update ship commodity
							$.commodities.ship_goods = ', $playerinfo['ship_goods'], ' + transfer_goods * 1;
						}
						
						// Update free holds display
						$.commodities.free_holds = $.commodities.max_holds - $.commodities.ship_ore - $.commodities.ship_organics - $.commodities.ship_goods - $.commodities.ship_colonists;
						$("#free_holds").html(addCommas($.commodities.free_holds));
						
						// Update planet commodity display
						$("#planet_goods").html(addCommas($.commodities.planet_goods));
						
						// Update ship commodity display
						$("#ship_goods").html(addCommas($.commodities.ship_goods));
						
						// Update this slider with the adjusted transfer amount, relative to the ship load
						$("#slider_goods").slider("option", "value", $.commodities.ship_goods * 1 - ', $playerinfo['ship_goods'], ' * 1);
						
						// Update the transfer input field with the adjusted transfer amount, relative the ship load
						$("#manual_transfer_goods").val(addCommas($.commodities.ship_goods * 1 - ', $playerinfo['ship_goods'], ' * 1));
						
						// Update other commodity sliders that use the ships holds
						UpdateCargoSliders("goods");
					}
					
					// Manual Colonists update
					function UpdateColonists(transfer_colonists)
					{	
						// Check if the player tries to transfer more of the commodity to his ship than is on the planet
						if (transfer_colonists > $.commodities.planet_colonists)
						{
							// Check if the player is trying to transfer more of the commoditiy to his ship than he has hold space for
							if (transfer_colonists > $.commodities.free_holds)
							{
								// Update the transfer amount to whatever the player can hold
								transfer_colonists =  $.commodities.free_holds;
								
								// Update the planet value
								$.commodities.planet_colonists = $.commodities.planet_colonists * 1 - transfer_colonists * 1;
								
								// Updaet ship value
								$.commodities.ship_colonists = $.commodities.ship_colonists * 1 + transfer_colonists * 1;
							}
							// Otherwise, update ship commodity
							else
							{
								// Set the ship to the current commodity load plus everything on the planet
								$.commodities.ship_colonists = $.commodities.planet_colonists * 1 + $.commodities.ship_colonists * 1;
								
								// Set the planet commodity to 0
								$.commodities.planet_colonists = 0;
							}
						}
						// Check if the player tries to transfer more of the commodity to the planet than is on his ship
						else if (transfer_colonists < -(', $playerinfo['ship_colonists'], ' * 1))
						{
							transfer_colonists = $.commodities.ship_colonists * -1;
							
							// Update planet commodity
							$.commodities.planet_colonists = $.commodities.planet_colonists * 1 + $.commodities.ship_colonists * 1;
							
							// Update ship commodity
							$.commodities.ship_colonists = 0;
						}
						// Check if the player tries to transfer more of the commodity to his ship than he has holds for
						else if (transfer_colonists > $.commodities.free_holds)
						{
							transfer_colonists = $.commodities.free_holds;
							
							// Update planet commodity
							$.commodities.planet_colonists = $.commodities.planet_colonists - transfer_colonists * 1;
							
							// Update ship commodity
							$.commodities.ship_colonists = $.commodities.ship_colonists + transfer_colonists * 1;
						}
						// Else the player is transferring a legal value
						else
						{
							// Update planet commodity
							$.commodities.planet_colonists = ', $planetinfo['colonists'], ' - transfer_colonists * 1;
							
							// Update ship commodity
							$.commodities.ship_colonists = ', $playerinfo['ship_colonists'], ' + transfer_colonists * 1;
						}
						
						// Update free holds display
						$.commodities.free_holds = $.commodities.max_holds - $.commodities.ship_ore - $.commodities.ship_organics - $.commodities.ship_goods - $.commodities.ship_colonists;
						$("#free_holds").html(addCommas($.commodities.free_holds));
						
						// Update planet commodity display
						$("#planet_colonists").html(addCommas($.commodities.planet_colonists));
						
						// Update ship commodity display
						$("#ship_colonists").html(addCommas($.commodities.ship_colonists));
						
						// Update this slider with the adjusted transfer amount, relative to the ship load
						$("#slider_colonists").slider("option", "value", $.commodities.ship_colonists * 1 - ', $playerinfo['ship_colonists'], ' * 1);
						
						// Update the transfer input field with the adjusted transfer amount, relative the ship load
						$("#manual_transfer_colonists").val(addCommas($.commodities.ship_colonists * 1 - ', $playerinfo['ship_colonists'], ' * 1));
						
						// Update other commodity sliders that use the ships holds
						UpdateCargoSliders("colonists");
					}
					
					// Manual Energy update
					function UpdateEnergy(transfer_energy)
					{	
						// Check if the player tries to transfer more of the commodity to his ship than is on the planet
						if (transfer_energy > $.commodities.planet_energy)
						{
							// Check if the player is trying to transfer more of the commoditiy to his ship than he has hold space for
							if (transfer_energy > $.commodities.free_power)
							{
								// Update the transfer amount to whatever the player can hold
								transfer_energy =  $.commodities.free_power;
								
								// Update the planet value
								$.commodities.planet_energy = $.commodities.planet_energy * 1 - transfer_energy * 1;
								
								// Updaet ship value
								$.commodities.ship_energy = $.commodities.ship_energy * 1 + transfer_energy * 1;
							}
							// Otherwise, update ship commodity
							else
							{
								// Set the ship to the current commodity load plus everything on the planet
								$.commodities.ship_energy = $.commodities.planet_energy * 1 + $.commodities.ship_energy * 1;
								
								// Set the planet commodity to 0
								$.commodities.planet_energy = 0;
							}
						}
						// Check if the player tries to transfer more of the commodity to the planet than is on his ship
						else if (transfer_energy < -(', $playerinfo['ship_energy'], ' * 1))
						{
							transfer_energy = $.commodities.ship_energy * -1;
							
							// Update planet commodity
							$.commodities.planet_energy = $.commodities.planet_energy * 1 + $.commodities.ship_energy * 1;
							
							// Update ship commodity
							$.commodities.ship_energy = 0;
						}
						// Check if the player tries to transfer more of the commodity to his ship than he has holds for
						else if (transfer_energy > $.commodities.free_power)
						{
							transfer_energy = $.commodities.free_power;
							
							// Update planet commodity
							$.commodities.planet_energy = $.commodities.planet_energy - transfer_energy * 1;
							
							// Update ship commodity
							$.commodities.ship_energy = $.commodities.ship_energy + transfer_energy * 1;
						}
						// Else the player is transferring a legal value
						else
						{
							// Update planet commodity
							$.commodities.planet_energy = ', $planetinfo['energy'], ' - transfer_energy * 1;
							
							// Update ship commodity
							$.commodities.ship_energy = ', $playerinfo['ship_energy'], ' + transfer_energy * 1;
						}
						
						// Update free power display
						$.commodities.free_power = $.commodities.max_power - $.commodities.ship_energy;
						$("#free_power").html(addCommas($.commodities.free_power));
						
						// Update planet commodity display
						$("#planet_energy").html(addCommas($.commodities.planet_energy));
						
						// Update ship commodity display
						$("#ship_energy").html(addCommas($.commodities.ship_energy));
						
						// Update this slider with the adjusted transfer amount, relative to the ship load
						$("#slider_energy").slider("option", "value", $.commodities.ship_energy * 1 - ', $playerinfo['ship_energy'], ' * 1);
						
						// Update the transfer input field with the adjusted transfer amount, relative the ship load
						$("#manual_transfer_energy").val(addCommas($.commodities.ship_energy * 1 - ', $playerinfo['ship_energy'], ' * 1));
					}

					// Manual Fighters update
					function UpdateFighters(transfer_fighters)
					{	
						// Check if the player tries to transfer more of the commodity to his ship than is on the planet
						if (transfer_fighters > $.commodities.planet_fighters)
						{
							// Check if the player is trying to transfer more of the commoditiy to his ship than he has hold space for
							if (transfer_fighters > $.commodities.free_fighters)
							{
								// Update the transfer amount to whatever the player can hold
								transfer_fighters =  $.commodities.free_fighters;
								
								// Update the planet value
								$.commodities.planet_fighters = $.commodities.planet_fighters * 1 - transfer_fighters * 1;
								
								// Updaet ship value
								$.commodities.ship_fighters = $.commodities.ship_fighters * 1 + transfer_fighters * 1;
							}
							// Otherwise, update ship commodity
							else
							{
								// Set the ship to the current commodity load plus everything on the planet
								$.commodities.ship_fighters = $.commodities.planet_fighters * 1 + $.commodities.ship_fighters * 1;
								
								// Set the planet commodity to 0
								$.commodities.planet_fighters = 0;
							}
						}
						// Check if the player tries to transfer more of the commodity to the planet than is on his ship
						else if (transfer_fighters < -(', $playerinfo['ship_fighters'], ' * 1))
						{
							transfer_fighters = $.commodities.ship_fighters * -1;
							
							// Update planet commodity
							$.commodities.planet_fighters = $.commodities.planet_fighters * 1 + $.commodities.ship_fighters * 1;
							
							// Update ship commodity
							$.commodities.ship_fighters = 0;
						}
						// Check if the player tries to transfer more of the commodity to his ship than he has holds for
						else if (transfer_fighters > $.commodities.free_fighters)
						{
							transfer_fighters = $.commodities.free_fighters;
							
							// Update planet commodity
							$.commodities.planet_fighters = $.commodities.planet_fighters - transfer_fighters * 1;
							
							// Update ship commodity
							$.commodities.ship_fighters = $.commodities.ship_fighters + transfer_fighters * 1;
						}
						// Else the player is transferring a legal value
						else
						{
							// Update planet commodity
							$.commodities.planet_fighters = ', $planetinfo['fighters'], ' - transfer_fighters * 1;
							
							// Update ship commodity
							$.commodities.ship_fighters = ', $playerinfo['ship_fighters'], ' + transfer_fighters * 1;
						}
						
						// Update free fighters
						$.commodities.free_fighters = $.commodities.max_fighters - $.commodities.ship_fighters;
						
						// Update planet commodity display
						$("#planet_fighters").html(addCommas($.commodities.planet_fighters));
						
						// Update ship commodity display
						$("#ship_fighters").html(addCommas($.commodities.ship_fighters));
						
						// Update this slider with the adjusted transfer amount, relative to the ship load
						$("#slider_fighters").slider("option", "value", $.commodities.ship_fighters * 1 - ', $playerinfo['ship_fighters'], ' * 1);
						
						// Update the transfer input field with the adjusted transfer amount, relative the ship load
						$("#manual_transfer_fighters").val(addCommas($.commodities.ship_fighters * 1 - ', $playerinfo['ship_fighters'], ' * 1));
					}
					
					// Manual Torps update
					function UpdateTorps(transfer_torps)
					{	
						// Check if the player tries to transfer more of the commodity to his ship than is on the planet
						if (transfer_torps > $.commodities.planet_torps)
						{
							// Check if the player is trying to transfer more of the commoditiy to his ship than he has hold space for
							if (transfer_torps > $.commodities.free_torps)
							{
								// Update the transfer amount to whatever the player can hold
								transfer_torps =  $.commodities.free_torps;
								
								// Update the planet value
								$.commodities.planet_torps = $.commodities.planet_torps * 1 - transfer_torps * 1;
								
								// Updaet ship value
								$.commodities.ship_torps = $.commodities.ship_torps * 1 + transfer_torps * 1;
							}
							// Otherwise, update ship commodity
							else
							{
								// Set the ship to the current commodity load plus everything on the planet
								$.commodities.ship_torps = $.commodities.planet_torps * 1 + $.commodities.ship_torps * 1;
								
								// Set the planet commodity to 0
								$.commodities.planet_torps = 0;
							}
						}
						// Check if the player tries to transfer more of the commodity to the planet than is on his ship
						else if (transfer_torps < -(', $playerinfo['torps'], ' * 1))
						{
							transfer_torps = $.commodities.ship_torps * -1;
							
							// Update planet commodity
							$.commodities.planet_torps = $.commodities.planet_torps * 1 + $.commodities.ship_torps * 1;
							
							// Update ship commodity
							$.commodities.ship_torps = 0;
						}
						// Check if the player tries to transfer more of the commodity to his ship than he has holds for
						else if (transfer_torps > $.commodities.free_torps)
						{
							transfer_torps = $.commodities.free_torps;
							
							// Update planet commodity
							$.commodities.planet_torps = $.commodities.planet_torps - transfer_torps * 1;
							
							// Update ship commodity
							$.commodities.ship_torps = $.commodities.ship_torps + transfer_torps * 1;
						}
						// Else the player is transferring a legal value
						else
						{
							// Update planet commodity
							$.commodities.planet_torps = ', $planetinfo['torps'], ' - transfer_torps * 1;
							
							// Update ship commodity
							$.commodities.ship_torps = ', $playerinfo['torps'], ' + transfer_torps * 1;
						}
						
						// Update free torps
						$.commodities.free_torps = $.commodities.max_torps - $.commodities.ship_torps;
						
						// Update planet commodity display
						$("#planet_torps").html(addCommas($.commodities.planet_torps));
						
						// Update ship commodity display
						$("#ship_torps").html(addCommas($.commodities.ship_torps));
						
						// Update this slider with the adjusted transfer amount, relative to the ship load
						$("#slider_torps").slider("option", "value", $.commodities.ship_torps * 1 - ', $playerinfo['torps'], ' * 1);
						
						// Update the transfer input field with the adjusted transfer amount, relative the ship load
						$("#manual_transfer_torps").val(addCommas($.commodities.ship_torps * 1 - ', $playerinfo['torps'], ' * 1));
					}
					
					// Manual Credits update
					function UpdateCredits(transfer_credits)
					{	
						// Check if the player tries to transfer more of the commodity to his ship than is on the planet
						if (transfer_credits > $.commodities.planet_credits)
						{
							// Check if the player is trying to transfer more of the commoditiy to his ship than he has hold space for
							if (transfer_credits > $.commodities.free_credits)
							{
								// Update the transfer amount to whatever the player can hold
								transfer_credits =  $.commodities.free_credits;
								
								// Update the planet value
								$.commodities.planet_credits = $.commodities.planet_credits * 1 - transfer_credits * 1;
								
								// Updaet ship value
								$.commodities.ship_credits = $.commodities.ship_credits * 1 + transfer_credits * 1;
							}
							// Otherwise, update ship commodity
							else
							{
								// Set the ship to the current commodity load plus everything on the planet
								$.commodities.ship_credits = $.commodities.planet_credits * 1 + $.commodities.ship_credits * 1;
								
								// Set the planet commodity to 0
								$.commodities.planet_credits = 0;
							}
						}
						// Check if the player tries to transfer more of the commodity to the planet than is on his ship
						else if (transfer_credits < -(', $playerinfo['credits'], ' * 1))
						{
							transfer_credits = $.commodities.ship_credits * -1;
							
							// Update planet commodity
							$.commodities.planet_credits = $.commodities.planet_credits * 1 + $.commodities.ship_credits * 1;
							
							// Update ship commodity
							$.commodities.ship_credits = 0;
						}
						// Else the player is transferring a legal value
						else
						{
							// Update planet commodity
							$.commodities.planet_credits = ', $planetinfo['credits'], ' - transfer_credits * 1;
							
							// Update ship commodity
							$.commodities.ship_credits = ', $playerinfo['credits'], ' + transfer_credits * 1;
						}
						
						// Update planet commodity display
						$("#planet_credits").html(addCommas($.commodities.planet_credits));
						
						// Update ship commodity display
						$("#ship_credits").html(addCommas($.commodities.ship_credits));
						
						// Update this slider with the adjusted transfer amount, relative to the ship load
						$("#slider_credits").slider("option", "value", $.commodities.ship_credits * 1 - ', $playerinfo['credits'], ' * 1);
						
						// Update the transfer input field with the adjusted transfer amount, relative the ship load
						$("#manual_transfer_credits").val(addCommas($.commodities.ship_credits * 1 - ', $playerinfo['credits'], ' * 1));
					}
					
					// Update other commodity sliders that use the ships holds
					function UpdateCargoSliders(source)
					{
						if ($.commodities.planet_ore > $.commodities.free_holds && source != "ore")
						{
							$("#slider_ore").slider("option", "max", $("#slider_ore").slider("option", "value") + $.commodities.free_holds);
							$("#slider_ore").slider("option", "value", $("#slider_ore").slider("option", "value"));
						}
						else if (source != "ore")
						{
							$("#slider_ore").slider("option", "max", $.commodities.planet_ore);
							$("#slider_ore").slider("option", "value", $("#slider_ore").slider("option", "value"));
						}
						
						if ($.commodities.planet_organics > $.commodities.free_holds && source != "organics")
						{
							$("#slider_organics").slider("option", "max", $("#slider_organics").slider("option", "value") + $.commodities.free_holds);
							$("#slider_organics").slider("option", "value", $("#slider_organics").slider("option", "value"));
						}
						else if (source != "organics")
						{
							$("#slider_organics").slider("option", "max", $.commodities.planet_organics);
							$("#slider_organics").slider("option", "value", $("#slider_organics").slider("option", "value"));
						}
						
						if ($.commodities.planet_goods > $.commodities.ship_goods && source != "goods")
						{
							$("#slider_goods").slider("option", "max", $("#slider_goods").slider("option", "value") + $.commodities.free_holds);
							$("#slider_goods").slider("option", "value", $("#slider_goods").slider("option", "value"));
						}
						else if (source != "goods")
						{
							$("#slider_goods").slider("option", "max", $.commodities.planet_goods);
							$("#slider_goods").slider("option", "value", $("#slider_goods").slider("option", "value"));
						}
						
						if ($.commodities.planet_colonists > $.commodities.ship_colonists && source != "colonists")
						{
							$("#slider_colonists").slider("option", "max", $("#slider_colonists").slider("option", "value") + $.commodities.free_holds);
							$("#slider_colonists").slider("option", "value", $("#slider_colonists").slider("option", "value"));
						}
						else if (source != "colonists")
						{
							$("#slider_colonists").slider("option", "max", $.commodities.planet_colonists);
							$("#slider_colonists").slider("option", "value", $("#slider_colonists").slider("option", "value"));
						}
					}
				});
			</script>
			<form id="ptransfer">
				<table id="planet_transfer_table" class="bnt_table">
					<thead>
						<tr>
							<th>', $txt['bnt_commodity'], '</th>
							<th>', $txt['bnt_planet'], '</th>
							<th></th>
							<th>', $txt['bnt_amount'], '</th>
							<th></th>
							<th>', $txt['bnt_ship'], '</th> 
							<th>', $txt['bnt_planet_transfertofrom'], '</th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td class="commodity_type">', $txt['bnt_ore'], '</td>
							<td class="planet_commodity_col"><span id="planet_ore">', Number($planetinfo['ore']), '</span></td>
							<td class="transfer_icon_col"><div class="max_toplanet" title="Transfer max to Planet"><input id="max_ship_ore" type="hidden" value="-', $playerinfo['ship_ore'], '" /><span class="ui-icon ui-icon-circle-arrow-w"></span></div></td>
							<td><div id="slider_ore" class="transfer_slider"></div></td>
							<td class="transfer_icon_col"><div class="max_toship" title="Transfer max to Ship"><input id="max_planet_ore" type="hidden" value="', $planetinfo['ore'], '" /><span class="ui-icon ui-icon-circle-arrow-e"></div></span></td>
							<td class="ship_commodity_col"><span id="ship_ore">', Number($playerinfo['ship_ore']), '</span></td>
							<td class="manual_input_col"><input type="text" id="manual_transfer_ore" name="transfer_ore" size="15" maxlength="20" /></td>
						</tr>
						<tr>
							<td class="commodity_type">', $txt['bnt_organics'], '</td>
							<td class="planet_commodity_col"><span id="planet_organics">', Number($planetinfo['organics']), '</span></td>
							<td class="transfer_icon_col"><div class="max_toplanet" title="Transfer max to Planet"><input id="max_ship_organics" type="hidden" value="-', $playerinfo['ship_organics'], '" /><span class="ui-icon ui-icon-circle-arrow-w"></span></div></td>
							<td><div id="slider_organics" class="transfer_slider"></div></td>
							<td class="transfer_icon_col"><div class="max_toship" title="Transfer max to Ship"><input id="max_planet_organics" type="hidden" value="', $planetinfo['organics'], '" /><span class="ui-icon ui-icon-circle-arrow-e"></span></div></td>
							<td class="ship_commodity_col"><span id="ship_organics">', Number($playerinfo['ship_organics']), '</span></td>
							<td class="manual_input_col"><input type="text" id="manual_transfer_organics" name="transfer_organics" size="15" maxlength="20" /></td>
						</tr>
						<tr>
							<td class="commodity_type">', $txt['bnt_goods'], '</td>
							<td class="planet_commodity_col"><span id="planet_goods">', Number($planetinfo['goods']), '</span></td>
							<td class="transfer_icon_col"><div class="max_toplanet" title="Transfer max to Planet"><input id="max_ship_goods" type="hidden" value="-', $playerinfo['ship_goods'], '" /><span class="ui-icon ui-icon-circle-arrow-w"></span></div></td>
							<td><div id="slider_goods" class="transfer_slider"></div></td>
							<td class="transfer_icon_col"><div class="max_toship" title="Transfer max to Ship"><input id="max_planet_goods" type="hidden" value="', $planetinfo['goods'], '" /><span class="ui-icon ui-icon-circle-arrow-e"></span></div></td>
							<td class="ship_commodity_col"><span id="ship_goods">', Number($playerinfo['ship_goods']), '</span></td>
							<td class="manual_input_col"><input type="text" id="manual_transfer_goods" name="transfer_goods" size="15" maxlength="20" /></td>
						</tr>
						<tr>
							<td class="commodity_type">', $txt['bnt_colonists'], '</td>
							<td class="planet_commodity_col"><span id="planet_colonists">', Number($planetinfo['colonists']), '</span></td>
							<td class="transfer_icon_col"><div class="max_toplanet" title="Transfer max to Planet"><input id="max_ship_colonists" type="hidden" value="-', $playerinfo['ship_colonists'], '" /><span class="ui-icon ui-icon-circle-arrow-w"></span></div></td>
							<td><div id="slider_colonists" class="transfer_slider"></div></td>
							<td class="transfer_icon_col"><div class="max_toship" title="Transfer max to Ship"><input id="max_planet_colonists" type="hidden" value="', $planetinfo['colonists'], '" /><span class="ui-icon ui-icon-circle-arrow-e"></span></div></td>
							<td class="ship_commodity_col"><span id="ship_colonists">', Number($playerinfo['ship_colonists']), '</span></td>
							<td class="manual_input_col"><input type="text" id="manual_transfer_colonists" name="transfer_colonists" size="15" maxlength="20" /></td>
						</tr>
						<tr>
							<td class="commodity_type">', $txt['bnt_energy'], '</td>
							<td class="planet_commodity_col"><span id="planet_energy">', Number($planetinfo['energy']), '</span></td>
							<td class="transfer_icon_col"><div class="max_toplanet" title="Transfer max to Planet"><input id="max_ship_energy" type="hidden" value="-', $playerinfo['ship_energy'], '" /><span class="ui-icon ui-icon-circle-arrow-w"></span></div></td>
							<td><div id="slider_energy" class="transfer_slider"></div></td>
							<td class="transfer_icon_col"><div class="max_toship" title="Transfer max to Ship"><input id="max_planet_energy" type="hidden" value="', $planetinfo['energy'], '" /><span class="ui-icon ui-icon-circle-arrow-e"></span></div></td>
							<td class="ship_commodity_col"><span id="ship_energy">', Number($playerinfo['ship_energy']), '</span></td>
							<td class="manual_input_col"><input type="text" id="manual_transfer_energy" name="transfer_energy" size="15" maxlength="20" /></td>
						</tr>
						<tr>
							<td class="commodity_type">', $txt['bnt_fighters'], '</td>
							<td class="planet_commodity_col"><span id="planet_fighters">', Number($planetinfo['fighters']), '</span></td>
							<td class="transfer_icon_col"><div class="max_toplanet" title="Transfer max to Planet"><input id="max_ship_fighters" type="hidden" value="-', $playerinfo['ship_fighters'], '" /><span class="bnt_icon ui-icon ui-icon-circle-arrow-w"></span></div></td>
							<td><div id="slider_fighters" class="transfer_slider"></div></td>
							<td class="transfer_icon_col"><div class="max_toship" title="Transfer max to Ship"><input id="max_planet_fighters" type="hidden" value="', $planetinfo['fighters'], '" /><span class="bnt_icon ui-icon ui-icon-circle-arrow-e"></span></div></td>
							<td class="ship_commodity_col"><span id="ship_fighters">', Number($playerinfo['ship_fighters']), '</ship></td>
							<td class="manual_input_col"><input type="text" id="manual_transfer_fighters" name="transfer_fighters" size="15" maxlength="20" /></td>
						</tr>
						<tr>
							<td class="commodity_type">', $txt['bnt_torps'], '</td>
							<td class="planet_commodity_col"><span id="planet_torps">', Number($planetinfo['torps']), '</span></td>
							<td class="transfer_icon_col"><div class="max_toplanet" title="Transfer max to Planet"><input id="max_ship_torps" type="hidden" value="-', $playerinfo['torps'], '" /><span class="ui-icon ui-icon-circle-arrow-w"></span></div></td>
							<td><div id="slider_torps" class="transfer_slider"></div></td>
							<td class="transfer_icon_col"><div class="max_toship" title="Transfer max to Ship"><input id="max_planet_torps" type="hidden" value="', $planetinfo['torps'], '" /><span class="ui-icon ui-icon-circle-arrow-e"></span></div></td>
							<td class="ship_commodity_col"><span id="ship_torps">', Number($playerinfo['torps']), '</span></td>
							<td class="manual_input_col"><input type="text" id="manual_transfer_torps" name="transfer_torps" size="15" maxlength="20" /></td>
						</tr>
						<tr >
							<td class="commodity_type">', $txt['bnt_credits'], '</td>
							<td class="planet_commodity_col"><span id="planet_credits">', Number($planetinfo['credits']), '</span></td>
							<td class="transfer_icon_col"><div class="max_toplanet" title="Transfer max to Planet"><input id="max_ship_credits" type="hidden" value="-', $playerinfo['credits'], '" /><span class="ui-icon ui-icon-circle-arrow-w"></span></div></td>
							<td><div id="slider_credits" class="transfer_slider"></div></td>
							<td class="transfer_icon_col"><div class="max_toship" title="Transfer max to Ship"><input id="max_planet_credits" type="hidden" value="', $planetinfo['credits'], '" /><span class="ui-icon ui-icon-circle-arrow-e"></span></div></td>
							<td class="ship_commodity_col"><span id="ship_credits">', Number($playerinfo['credits']), '</span></td>
							<td class="manual_input_col"><input type="text" id="manual_transfer_credits" name="transfer_credits" size="15" maxlength="20" /></td>
						</tr>
					</tbody>
				</table>
				<div id="transfer_button"><span id="returnto_planet_menu" class="bnt_button ui-state-default ui-corner-all"><input id="planet_id" type="hidden" value="', $planetinfo['planet_id'], '">', $txt['bnt_planet_title'], '</span></div>
				<div id="transfer_button"><span id="reset_ptransfer" class="bnt_button ui-state-default ui-corner-all" title="command=transfer"><input id="planet_id" type="hidden" value="', $planetinfo['planet_id'], '">', $txt['bnt_reset'], '</span></div>
				<div id="transfer_button"><span id="initiate_ptransfer" class="bnt_button ui-state-default ui-corner-all">', $txt['bnt_planet_transfer_link'], '</span></div>
			</form>
			<div id="planet_debug" class="clearfix"></div>';
}

function ProcessTransferRequest($playerinfo, $planetinfo)
{

	global $smcFunc, $txt;
	global $dbtables;
	global $max_credits_without_base, $corp_planet_transfers;

	if ($planetinfo['sector_id'] <> $playerinfo['sector'])
	{
		echo '
			<p>', $txt['bnt_planet2_sector'], '</p>';
		return;
	}
	else if ($playerinfo['turns'] < 1)
	{
		echo '
			<p>', $txt['bnt_planet2_noturn'], '</p>';
		return;
	}
	else
	{
		$free_holds = NUM_HOLDS($playerinfo['hull']) - $playerinfo['ship_ore'] - $playerinfo['ship_organics'] - $playerinfo['ship_goods'] - $playerinfo['ship_colonists'];
		$free_power = NUM_ENERGY($playerinfo['power']) - $playerinfo['ship_energy'];
		$fighter_max = NUM_FIGHTERS($playerinfo['computer']) - $playerinfo['ship_fighters'];
		$torpedo_max = NUM_TORPEDOES($playerinfo['torp_launchers']) - $playerinfo['torps'];

		// Store the GET variables
		$transfer_ore = $_GET['transfer_ore'];
		$transfer_organics = $_GET['transfer_organics'];
		$transfer_goods = $_GET['transfer_goods'];
		$transfer_energy = $_GET['transfer_energy'];
		$transfer_colonists = $_GET['transfer_colonists'];
		$transfer_credits = $_GET['transfer_credits'];
		$transfer_torps = $_GET['transfer_torps'];
		$transfer_fighters = $_GET['transfer_fighters'];
		
		// Strip all commas
		$transfer_ore = str_replace(",", "", $transfer_ore);
		$transfer_organics = str_replace(",", "", $transfer_organics);
		$transfer_goods = str_replace(",", "", $transfer_goods);
		$transfer_energy = str_replace(",", "", $transfer_energy);
		$transfer_colonists = str_replace(",", "", $transfer_colonists);
		$transfer_credits = str_replace(",", "", $transfer_credits);
		$transfer_torps = str_replace(",", "", $transfer_torps);
		$transfer_fighters = str_replace(",", "", $transfer_fighters);
		
		// Now multiply all the transfer amounts by 1 to eliminate any trailing spaces.
		$transfer_ore *= 1;
		$transfer_organics *= 1;
		$transfer_goods *= 1;
		$transfer_energy *= 1;
		$transfer_colonists *= 1;
		$transfer_credits *= 1;
		$transfer_torps *= 1;
		$transfer_fighters *= 1;

		// Make sure that the source for each commodity transfer has sufficient numbers to fill the transfer.
		if ($transfer_ore < 0 && abs($transfer_ore) > $playerinfo['ship_ore'])
		{
			$transfer_ore = -($playerinfo['ship_ore']);
			echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_ore'], '. ', $txt['bnt_planet2_settr'], ' ', $transfer_ore, ' ', $txt['bnt_units'], ' ', $txt['bnt_ore'], '.<br>';
		}
		elseif ($transfer_ore > 0 && $transfer_ore > $planetinfo['ore'])
		{
			$transfer_ore = $planetinfo['ore'];
			echo $txt['bnt_planet2_losup'], ' ', $transfer_ore, ' ', $txt['bnt_units'], ' ', $txt['bnt_ore'], '.<br>';
		}

		if ($transfer_organics < 0 && abs($transfer_organics) > $playerinfo['ship_organics'])
		{
			$transfer_organics = -($playerinfo['ship_organics']);
			echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_organics'], '. ', $txt['bnt_planet2_settr'], ' ', $transfer_organics, ' ', $txt['bnt_units'], ' ', $txt['bnt_organics'], '.<br>';
		}
		elseif ($transfer_organics > 0 && $transfer_organics > $planetinfo['organics'])
		{
			$transfer_organics = $planetinfo['organics'];
			echo $txt['bnt_planet2_losup'], ' ', $transfer_organics, ' ', $txt['bnt_units'], ' ', $txt['bnt_organics'], '.<br>';
		}

		if ($transfer_goods < 0 && abs($transfer_goods) > $playerinfo['ship_goods'])
		{
			$transfer_goods = -($playerinfo['ship_goods']);
			echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_goods'], '. ', $txt['bnt_planet2_settr'], ' ', $transfer_goods, ' ', $txt['bnt_units'], ' ', $txt['bnt_goods'], '.<br>';
		}
		elseif ($transfer_goods > 0 && $transfer_goods > $planetinfo['goods'])
		{
			$transfer_goods = $planetinfo['goods'];
			echo $txt['bnt_planet2_losup'], ' ', $transfer_goods, ' ', $txt['bnt_units'], ' ', $txt['bnt_goods'], '.<br>';
		}

		if ($transfer_energy < 0 && abs($transfer_energy) > $playerinfo['ship_energy'])
		{
			$transfer_energy = -($playerinfo['ship_energy']);
			echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_energy'], '. ', $txt['bnt_planet2_settr'], ' ', $transfer_energy, ' ', $txt['bnt_units'], ' ', $txt['bnt_energy'], '.<br>';
		}
		elseif ($transfer_energy > 0 && $transfer_energy > $planetinfo['energy'])
		{
			$transfer_energy = $planetinfo['energy'];
			echo $txt['bnt_planet2_losup'], ' ', $transfer_energy, ' ', $txt['bnt_units'], ' ', $txt['bnt_energy'], '.<br>';
		}

		if ($transfer_colonists < 0 && abs($transfer_colonists) > $playerinfo['ship_colonists'])
		{
			$transfer_colonists = -($playerinfo['ship_colonists']);
			echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_colonists'], '. ', $txt['bnt_planet2_settr'], ' ', $transfer_colonists, ' ', $txt['bnt_units'], ' ', $txt['bnt_colonists'], '.<br>';
		}
		elseif ($transfer_colonists > 0 && $transfer_colonists > $planetinfo['colonists'])
		{
			$transfer_colonists = $planetinfo['colonists'];
			echo $txt['bnt_planet2_losup'], ' ', $transfer_colonists, ' ', $txt['bnt_units'], ' ', $txt['bnt_colonists'], '.<br>';
		}

		if ($transfer_credits < 0 && abs($transfer_credits) > $playerinfo['credits'])
		{
			$transfer_credits = -($playerinfo['credits']);
			echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_credits'], '. ', $txt['bnt_planet2_settr'], ' ', $transfer_credits, ' ', $txt['bnt_units'], ' ', $txt['bnt_credits'], '.<br>';
		}
		elseif ($transfer_credits > 0 && $transfer_credits > $planetinfo['credits'])
		{
			$transfer_credits = $planetinfo['credits'];
			echo $txt['bnt_planet2_losup'], ' ', $transfer_credits, ' ', $txt['bnt_units'], ' ', $txt['bnt_credits'], '.<br>';
		}

		if ($planetinfo['base'] == 'N' && ($transfer_credits - $planetinfo['credits'] < -($max_credits_without_base)))
		{
			$transfer_credits = MAX($max_credits_without_base - $planetinfo['credits'], 0);
			echo $txt['bnt_planet2_baseexceeded'], ' ', $txt['bnt_planet2_settr'], ' ',  $transfer_credits, ' ',  $txt['bnt_credits'], '.<br>';
		}

		if ($transfer_torps < 0 && abs($transfer_torps) > $playerinfo['torps'])
		{
			$transfer_torps = -($playerinfo['torps']);
			echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_torps'], '. ', $txt['bnt_planet2_settr'], ' ', $transfer_torps, ' ', $txt['bnt_units'], ' ', $txt['bnt_torps'], '.<br>';
		}
		elseif ($transfer_torps > 0 && $transfer_torps > $planetinfo['torps'])
		{
			$transfer_torps = $planetinfo['torps'];
			echo $txt['bnt_planet2_losup'], ' ', $transfer_torps, ' ', $txt['bnt_units'], ' ', $txt['bnt_torps'], '.<br>';
		}

		if ($transfer_fighters < 0 && abs($transfer_fighters) > $playerinfo['ship_fighters'])
		{
			$transfer_fighters = -($playerinfo['ship_fighters']);
			echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_fighters'], '. ', $txt['bnt_planet2_settr'], ' ', $transfer_fighters, ' ', $txt['bnt_units'], ' ', $txt['bnt_fighters'], '.<br>';
		}
		elseif ($transfer_fighters > 0 && $transfer_fighters > $planetinfo['fighters'])
		{
			$transfer_fighters = $planetinfo['fighters'];
			echo $txt['bnt_planet2_losup'], ' ', $transfer_fighters, ' ', $txt['bnt_units'], ' ', $txt['bnt_fighters'], '.<br>';
		}

		$total_holds_needed = $transfer_ore + $transfer_organics + $transfer_goods + $transfer_colonists;

		if ($playerinfo['ship_id'] != $planetinfo['owner'] && $transfer_credits != 0 && $corp_planet_transfers != 1)
		{
			echo '
				<p>', $txt['bnt_planet2_nocorptransfer'], '</p>';
		}
		elseif ($total_holds_needed > $free_holds)
		{
			echo '
				<p>', $txt['bnt_planet2_noten'], ' ', $txt['bnt_holds'], ' ', $txt['bnt_planet2_fortr'], '</p>';
		}
		else
		{
			if (!empty($planetinfo))
			{
				if ($planetinfo['owner'] == $playerinfo['ship_id'] || ($planetinfo['corp'] == $playerinfo['team'] && $playerinfo['team'] <> 0))
				{
					if ($transfer_ore < 0 && $playerinfo['ship_ore'] < abs($transfer_ore))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_ore'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_ore = 0;
					}
					elseif ($transfer_ore > 0 && $planetinfo['ore'] < abs($transfer_ore))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_ore'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_ore = 0;
					}
					
					if( $transfer_organics < 0 && $playerinfo['ship_organics'] < abs($transfer_organics))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_organics'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_organics = 0;
					}
					elseif ($transfer_organics > 0 && $planetinfo['organics'] < abs($transfer_organics))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_organics'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_organics = 0;
					}
					
					if ($transfer_goods < 0 && $playerinfo['ship_goods'] < abs($transfer_goods))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_goods'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_goods = 0;
					}
					elseif ($transfer_goods > 0 && $planetinfo['goods'] < abs($transfer_goods))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_goods'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_goods = 0;
					}
					
					if ($transfer_energy < 0 && $playerinfo['ship_energy'] < abs($transfer_energy))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_energy'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_energy = 0;
					}
					elseif ($transfer_energy > 0 && $planetinfo['energy'] < abs($transfer_energy))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_energy'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_energy = 0;
					}
					elseif ($transfer_energy > 0 && abs($transfer_energy) > $free_power)
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_planet2_power'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_energy = 0;
					}
					
					if ($transfer_colonists < 0 && $playerinfo['ship_colonists'] < abs($transfer_colonists))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_colonists'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_colonists = 0;
					}
					elseif ($transfer_colonists > 0 && $planetinfo['colonists'] < abs($transfer_colonists))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_colonists'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_colonists = 0;
					}
					
					if ($transfer_fighters < 0 && $playerinfo['ship_fighters'] < abs($transfer_fighters))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_fighters'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_fighters = 0;
					}
					elseif ($transfer_fighters > 0 && $planetinfo['fighters'] < abs($transfer_fighters))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_fighters'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_fighters = 0;
					}
					elseif ($transfer_fighters > 0 && abs($transfer_fighters) > $fighter_max)
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_planet2_comp'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_fighters = 0;
					}
			
					if ($transfer_torps < 0 && $playerinfo['torps'] < abs($transfer_torps))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_torpedoes'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_torps = 0;
					}
					elseif ($transfer_torps > 0 && $planetinfo['torps'] < abs($transfer_torps))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_torpedoes'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_torps = 0;
					}
					elseif ($transfer_torps > 0 && abs($transfer_torps) > $torpedo_max)
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_planet2_laun'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_torps = 0;
					}
			
					if($transfer_credits < 0 && $playerinfo['credits'] < abs($transfer_credits))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_credits'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_credits = 0;
					}
					elseif($transfer_credits > 0 && $planetinfo['credits'] < abs($transfer_credits))
					{
						echo $txt['bnt_planet2_noten'], ' ', $txt['bnt_credits'], ' ', $txt['bnt_planet2_fortr'], '<br>';
						$transfer_credits = 0;
					}
					
					// Update player ship supplies.
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET ship_ore = ship_ore + {int:transfer_ore},
							ship_organics = ship_organics + {int:transfer_organics},
							ship_goods = ship_goods + {int:transfer_goods},
							ship_energy = ship_energy + {int:transfer_energy},
							ship_colonists = ship_colonists + {int:transfer_colonists},
							torps = torps + {int:transfer_torps},
							ship_fighters = ship_fighters + {int:transfer_fighters},
							credits = credits + {float:transfer_credits},
							turns = turns - 1,
							turns_used = turns_used + 1 
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'ship_id' => $playerinfo['ship_id'],
							'transfer_ore' => $transfer_ore,
							'transfer_organics' => $transfer_organics,
							'transfer_goods' => $transfer_goods,
							'transfer_energy' => $transfer_energy,
							'transfer_colonists' => $transfer_colonists,
							'transfer_torps' => $transfer_torps,
							'transfer_fighters' => $transfer_fighters,
							'transfer_credits' => round($transfer_credits)
						)
					);
					
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_planets}
						SET ore = ore - {int:transfer_ore},
							organics = organics - {int:transfer_organics},
							goods = goods - {int:transfer_goods},
							energy = energy - {int:transfer_energy},
							colonists = colonists - {int:transfer_colonists},
							torps = torps - {int:transfer_torps},
							fighters = fighters - {int:transfer_fighters},
							credits = credits - {float:transfer_credits}
						WHERE planet_id = {int:planet_id}',
						array(
							'db_planets' => $dbtables['planets'],
							'planet_id' => $planetinfo['planet_id'],
							'transfer_ore' => $transfer_ore,
							'transfer_organics' => $transfer_organics,
							'transfer_goods' => $transfer_goods,
							'transfer_energy' => $transfer_energy,
							'transfer_colonists' => $transfer_colonists,
							'transfer_torps' => $transfer_torps,
							'transfer_fighters' => $transfer_fighters,
							'transfer_credits' => round($transfer_credits)
						)
					);
					
					// Refresh player info
					$result = $smcFunc['db_query']('', '
						SELECT *
						FROM {raw:db_ships}
						WHERE user_id = {int:user_id}
						LIMIT 1',
						array(
							'db_ships' => $dbtables['ships'],
							'user_id' => $playerinfo['user_id'],
						)
					);
					$playerinfo = $smcFunc['db_fetch_assoc']($result);
					$smcFunc['db_free_result']($result);
					
					echo '
					<div id="ptransfer_result_frame">
						<div class="overlay"></div>
						<div id="ptransfer_result_content">
							<h4>', $txt['bnt_planet2_compl'], '</h4>';
					
					if ($transfer_ore > 0)
						echo '
						<p><strong>', Number($transfer_ore), '</strong> ', $txt['bnt_ore'], ' ', $txt['bnt_planet2_toship'], '</p>';
					elseif ($transfer_ore < 0)
						echo '
						<p><strong>', Number(abs($transfer_ore)), '</strong> ', $txt['bnt_ore'], ' ', $txt['bnt_planet2_toplanet'], '</p>';
						
					if ($transfer_organics > 0)
						echo '
						<p><strong>', Number($transfer_organics), '</strong> ', $txt['bnt_organics'], ' ', $txt['bnt_planet2_toship'], '</p>';
					elseif ($transfer_organics < 0)
						echo '
						<p><strong>', Number(abs($transfer_organics)), '</strong> ', $txt['bnt_organics'], ' ', $txt['bnt_planet2_toplanet'], '</p>';
						
					if ($transfer_goods > 0)
						echo '
						<p><strong>', Number($transfer_goods), '</strong> ', $txt['bnt_goods'], ' ', $txt['bnt_planet2_toship'], '</p>';
					else if ($transfer_goods < 0)
						echo '
						<p><strong>', Number(abs($transfer_goods)), '</strong> ', $txt['bnt_goods'], ' ', $txt['bnt_planet2_toplanet'], '</p>';
					
					if ($transfer_energy > 0)
						echo '
						<p><strong>', Number($transfer_energy), '</strong> ', $txt['bnt_energy'], ' ',  $txt['bnt_planet2_toship'], '</p>';
					else if ($transfer_energy < 0)
						echo '
						<p><strong>', Number(abs($transfer_energy)), '</strong> ', $txt['bnt_energy'], ' ', $txt['bnt_planet2_toplanet'], '</p>';
					
					if ($transfer_colonists > 0)
						echo '
						<p><strong>', Number($transfer_colonists), '</strong> ', $txt['bnt_colonists'], ' ', $txt['bnt_planet2_toship'], '</p>';
					else if ($transfer_colonists < 0)
						echo '
						<p><strong>', Number(abs($transfer_colonists)), '</strong> ', $txt['bnt_colonists'], ' ',  $txt['bnt_planet2_toplanet'], '</p>';
					
					if ($transfer_fighters > 0)
						echo '
						<p><strong>', Number($transfer_fighters), '</strong> ', $txt['bnt_fighters'], ' ', $txt['bnt_planet2_toship'], '</p>';
					else if ($transfer_fighters < 0)
						echo '
						<p><strong>', Number(abs($transfer_fighters)), '</strong> ', $txt['bnt_fighters'], ' ', $txt['bnt_planet2_toplanet'], '</p>';
					
					if ($transfer_torps > 0)
						echo '
						<p><strong>', Number($transfer_torps), '</strong> ', $txt['bnt_torps'], ' ', $txt['bnt_planet2_toship'], '</p>';
					else if ($transfer_torps < 0)
						echo '
						<p><strong>', Number(abs($transfer_torps)), '</strong> ', $txt['bnt_torps'], ' ', $txt['bnt_planet2_toplanet'], '</p>';
						
					if ($transfer_credits > 0)
						echo '
						<p><strong>', Number($transfer_credits), '</strong> ', $txt['bnt_credits'], ' ', $txt['bnt_planet2_toship'], '</p>';
					else if ($transfer_credits < 0)
						echo '
						<p><strong>', Number(abs($transfer_credits)), '</strong> ', $txt['bnt_credits'], ' ', $txt['bnt_planet2_toplanet'], '</p>';
					
					echo '
						</div>
					</div>
					<div class="clearfix"></div>
					<div id="transfer_button"><span id="returnto_planet_menu" class="bnt_button ui-state-default ui-corner-all"><input id="planet_id" type="hidden" value="', $planetinfo['planet_id'], '">', $txt['bnt_planet_title'], '</span></div>			
					<input id="planet_id" type="hidden" value="', $planetinfo['planet_id'], '">
					<input id="updated_turns_available" type="hidden" value="', Number($playerinfo['turns']), '">
					<input id="updated_turns_used" type="hidden" value="', Number($playerinfo['turns_used']), '">
					<input id="ship_ore" type="hidden" value="', Number($playerinfo['ship_ore']), '">
					<input id="ship_organics" type="hidden" value="', Number($playerinfo['ship_organics']), '">
					<input id="ship_goods" type="hidden" value="', Number($playerinfo['ship_goods']), '">
					<input id="ship_energy" type="hidden" value="', Number($playerinfo['ship_energy']), '">
					<input id="ship_colonists" type="hidden" value="', Number($playerinfo['ship_colonists']), '">
					<input id="ship_torps" type="hidden" value="', Number($playerinfo['torps']), '">
					<input id="ship_fighters" type="hidden" value="', Number($playerinfo['ship_fighters']), '">
					<input id="ship_credits" type="hidden" value="', Number($playerinfo['credits']), '">';
				}
				else
					echo '
					<p>', $txt['bnt_planet2_notowner'], '</p>';
			}
			else
				echo '
					<p>', $txt['bnt_planet_none'], '</p>';
		}
	}
}
?>
